package in.muton.edushare.pages.coursepages;

import in.muton.edushare.url.EntityPageParams;
import in.muton.edushare.wicket.models.CayenneList;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.vo.AcademicYearModel;
import com.edushare.common.vo.CourseInstances;

public class CoursePrevInstances extends CourseBase {

	public CoursePrevInstances(PageParameters params) {
		super(params);

		WebMarkupContainer wmc = new WebMarkupContainer("prevSems");
		add(wmc);
		wmc.setVisible(getCourse().getObject().getCourseInstances().size() > 0);
		CayenneList<CourseInstances> cL = new CayenneList<CourseInstances>(
				getCourse().getObject().getCourseInstances());
		
		List<IModel<CourseInstances>> cInstList = new ArrayList<IModel<CourseInstances>>();
		List<IModel<CourseInstances>> origList = cL.getObject();
		for(IModel<CourseInstances> c:origList){
			if(c.getObject().getStatus() == CourseInstances.COURSE_OPEN_LINKED_TO_BATCH ||
					c.getObject().getStatus() == CourseInstances.COURSE_OPEN_STANDALONE)
				continue;
			cInstList.add(c);
		}
		

		wmc.add(new ListView<IModel<CourseInstances>>("instList", cInstList) {
			private static final long serialVersionUID = -4251643740755190966L;

			@Override
			protected void populateItem(ListItem<IModel<CourseInstances>> arg0) {
				Link<Void> l = new BookmarkablePageLink<Void>("instLink",
						CoursePage.class, EntityPageParams.getParams(
								new Model<CourseInstances>(arg0.getModelObject()
										.getObject()), arg0
										.getModelObject().getObject()
										.getCourseInstanceId().toString()));

				arg0.add(l);
				l.add(new Label("courseName", arg0.getModelObject().getObject()
						.getCourse().getCourseName()));
				AcademicYearModel a = arg0.getModelObject().getObject()
						.getSemester();
				arg0.add(new Label("semesterName", a.getSemesterName()));
				arg0.add(new Label("year", arg0.getModelObject().getObject()
						.getInstanceYear().toString()));
			}
		});

		WebMarkupContainer wmc2 = new WebMarkupContainer("noPrevSems");
		wmc2.setVisible(getCourse().getObject().getCourseInstances().size() == 0);
		add(wmc2);
	}

	@Override
	protected String getActivePage(PageParameters params) {
		return "CoursePrev";
	}

	@Override
	public String getPageTitle() {
		return getLoggedInUser().getCompleteUserName() + " | "
				+ getCourse().getObject().getCourseName()
				+ " in previous semesters | "
				+ ApplicationConstants.APPLICATION_NAME;
	}

}
